package com.joysuch.wwyt.core.controller;

import com.joysuch.wwyt.core.bean.AddConfigBean;
import com.joysuch.wwyt.core.bean.BaseConfigBean;
import com.joysuch.wwyt.core.bean.BaseConfigTypeBean;
import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.entity.BaseConfig;
import com.joysuch.wwyt.core.entity.BaseConfigType;
import com.joysuch.wwyt.core.service.BaseConfigService;
import com.joysuch.wwyt.core.support.Context;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.joysuch.wwyt.core.bean.BaseConfigBean.SearchBaseConfig;

/**
 * @author qingzhou
 * 2019-12-17
 */
@Slf4j
@RestController
@RequestMapping("/base/config")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = "系统管理-基础配置")
public class BaseConfigController {

    private final BaseConfigService service;

    @GetMapping("/get")
    public ResultBean get(String code) {
        return ResultBean.wrapSuccessfulResult(
                service.findConfig(Context.getCurrentTenantId(), Context.getCompanyCode(), code));
    }
    /*@GetMapping("/get")
    @ApiOperation(value = "查询")
    public ResultBean get(String code) {
        try {
            BaseConfig config = service.findConfig(Context.getCurrentTenantId(), Context.getCompanyCode(), code);
            return ResultBean.success(config);
        } catch (Exception e) {
            log.error("查询失败", e);
            return ResultBean.fail(101, "查询失败");
        }

    }*/

    @PostMapping("/saveType")
    @ApiOperation(value = "保存配置类型")
    @RequiresPermissions("base:config:saveType")
    public ResultBean saveType(@RequestBody BaseConfigTypeBean bean) {
            BaseConfigType type = service.saveType(bean);
            return ResultBean.success(type);
    }

    @GetMapping("/findTypeById")
    @ApiOperation(value = "查看配置类型")
    public ResultBean findTypeById(Long id) {
        BaseConfigTypeBean bean = service.findTypeById(id);
        return ResultBean.success(bean);
    }

    @GetMapping("/deleteTypeById")
    @ApiOperation(value = "删除配置类型")
    @RequiresPermissions("base:config:deleteTypeById")
    public ResultBean deleteTypeById(Long id) {
       service.deleteTypeById(id);
        return ResultBean.success("");
    }

    @GetMapping("/getTypeList")
    @ApiOperation(value = "获取配置类型列表")
    public ResultBean getTypeList() {
        List<BaseConfigTypeBean> list = service.getTypeList();
        return ResultBean.success(list);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增")
    @RequiresPermissions("base:config:add")
    public ResultBean add(@RequestBody AddConfigBean bean) {
        try {
            BaseConfig baseConfig = service.add(bean);
            return ResultBean.success(baseConfig);
        } catch (Exception e) {
            log.error("创建失败", e);
            return ResultBean.fail(101, "创建失败");
        }
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改")
    @RequiresPermissions("base:config:update")
    public ResultBean update(@RequestBody BaseConfig bean) {
        try {
            service.updateConfig(bean);
            return ResultBean.success("");
        } catch (IllegalArgumentException e) {
            log.error("修改失败", e);
            return ResultBean.fail(101, e.getMessage());
        } catch (Exception e) {
            log.error("修改失败", e);
            return ResultBean.fail(101, "修改失败");
        }
    }

    @GetMapping("/delete")
    @ApiOperation(value = "删除")
    @RequiresPermissions("base:config:delete")
    public ResultBean delete(Long id) {
        try {
            service.delete(id);
            return ResultBean.success("");
        } catch (IllegalArgumentException e) {
            log.error("删除失败", e);
            return ResultBean.fail(101, e.getMessage());
        }catch (Exception e) {
            log.error("删除失败", e);
            return ResultBean.fail(101, "删除失败");
        }
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultBean page(Pageable page, SearchBaseConfig condition) {
        try {
            Page<BaseConfigBean> p = service.findByPage(page,condition);
            return ResultBean.pageData(p.getContent(), p.getTotalElements());
        } catch (Exception e) {
            log.error("分页查询失败");
            return ResultBean.fail(101, "分页查询失败");
        }
    }
}
